<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/1/13
 * Time: 17:40
 * 特惠商品活动管理
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Activityh extends Base
{
    /**
     * 特惠商品活动首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $start_time = input('start_time');
            $end_time = input('end_time');
            $activity_title = input('activity_title');
            
            $where['activity_type'] = 2;
            if(isset($activity_title) && $activity_title != ''){
                $where['activity_title'] = ['like', '%' . $activity_title . '%'];
            }
            // 时间
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['activity_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['activity_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['activity_time'] = ['<', $end_time];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('activity')->where($where)->count();// 获取总条数
            $lists = Db::name('activity')
                ->field('activity_id, activity_title, activity_thumb, activity_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('activity_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加活动
     * @return mixed|\think\response\Json
     */
    public function activityAdd(){
        if(Request::instance()->isPost()){
            $activity_title = trim(input('activity_title'));
            $activity_thumb = input('activity_thumb');
            $activity_content = input('activity_content');
            
            $insert = [
                'activity_type' => 2,
                'activity_thumb' => $activity_thumb,
                'activity_title' => $activity_title,
                'activity_content' => $activity_content,
                'activity_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_activity')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('activityh/activityadd');
    }
    
    /**
     * 编辑活动
     * @return mixed|\think\response\Json
     */
    public function activityEdit(){
        $activity_id = input('activity_id');
        if(Request::instance()->isPost()){
            $activity_title = trim(input('activity_title'));
            $activity_thumb = input('activity_thumb');
            $activity_content = input('activity_content');
            
            $update = [
                'activity_thumb' => $activity_thumb,
                'activity_title' => $activity_title,
                'activity_content' => $activity_content,
                'activity_updatetime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_activity')->where('activity_id', $activity_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $activity = Db::table('up_activity')
            ->where('activity_id', $activity_id)
            ->field('activity_thumb, activity_title, activity_content')
            ->find();
        $this->assign([
            'activity' => $activity,
            'activity_id' => $activity_id
        ]);
        return $this->fetch('activityh/activityedit');
    }
    
    /**
     * 活动详情
     * @return mixed
     */
    public function activityDetail(){
        $activity_id = input('activity_id');
        $activity = Db::table('up_activity')
            ->field('activity_title, activity_thumb, activity_content')
            ->where('activity_id', $activity_id)
            ->find();
        return $this->fetch('activityh/activitydetail', ['activity' => $activity]);
    }
    
    /**
     * 删除活动
     * @return \think\response\Json
     */
    public function activityDel(){
        $activity_id = input('id');
        $res = Db::table('up_activity')->delete($activity_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}